package com.copractice.data.entity.performance;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.copractice.data.entity.PersistentObject;
import com.copractice.data.entity.answer.Answer;
import com.copractice.data.entity.feedback.UsersFeedback;

/**
 * Represents data for each users attempts when he/she is answering the question.
 *
 * @author Dmitry Churbanov (dmitry.churbanov@gmail.com)
 * @version 1.0 04/15/2009
 */
@Entity
@Table(name = "USERS_QUESTIONS_ATTEMPTS")
@NamedQueries(
        {
            @NamedQuery(name="UsersQuestionsAttempts.findByUserPerformanceAndAswerId",
                        query="select uqa from UsersQuestionsAttempts uqa where uqa.answer.id = ? AND" +
                              " uqa.usersPerformance.id = ?")
        }
)
@SuppressWarnings("serial")
public class UsersQuestionsAttempts extends PersistentObject {

    /**
     * Users performance
     */
    private UsersPerformance usersPerformance;
    
    /**
     * Answer which user selected
     */
    private Answer answer;
    
    private List<UsersFeedback> feedback;
    
    /**
     * @see edu.vt.intelligenttutor.data.entity.PersistentObject#getId()
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Override
    public Long getId() {
        return super.getId();
    }

    /**
     * Returns user performance for current attempt
     * 
     * @return user performance for current attempt
     */
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name="USER_QUESTION_PERF_ID")
    public UsersPerformance getUsersPerformance()
    {
        return usersPerformance;
    }

    /**
     * Sets user performance for current attempt
     * 
     * @param usersPerformance
     */
    public void setUsersPerformance(UsersPerformance usersPerformance) {
        this.usersPerformance = usersPerformance;
    }

    /**
     * Returns answer of current users attempt
     * 
     * @return
     */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="ANSWER_ID")
    public Answer getAnswer() {
        return answer;
    }

    public void setAnswer(Answer answer) {
        this.answer = answer;
    }    

    /**
     * Returns list of feedbacks user saw during answering the question
     * 
     * @return List of <code>UsersFeedback</code>
     */
    @ManyToMany
    @JoinTable(name="ATTEMPTS_FEEDBACK")
    public List<UsersFeedback> getFeedback() {
        return feedback;
    }

    /**
     * Sets the list of <code>UsersFeedback</code> the user saw during answering the question
     * 
     * @param feedback List of <code>UsersFeedback</code>
     */
    public void setFeedback(List<UsersFeedback> feedback) {
        this.feedback = feedback;
    }    
    
}
